package com.project.lx.gettxtfilename;

import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;

public class AnimatorActivity extends AppCompatActivity {

    ImageView imageView_1, imageView_2, imageView_3, imageView_4, imageView_5, imageView_back;
    Button start, start_2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animator);

        imageView_1 = (ImageView) findViewById(R.id.image_1);
        imageView_2 = (ImageView) findViewById(R.id.image_2);
        imageView_3 = (ImageView) findViewById(R.id.image_3);
        imageView_4 = (ImageView) findViewById(R.id.image_4);
        imageView_5 = (ImageView) findViewById(R.id.image_5);
        imageView_back = (ImageView) findViewById(R.id.image_back);
        start = (Button) findViewById(R.id.btn_start);
        start_2 = (Button) findViewById(R.id.btn_start2);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startAnimation();
            }
        });
        start_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.schedule(task,0,1900);
            }
        });
    }

    public void startAnimation() {

        int width = imageView_1.getWidth();
        float traslation = width / 5;

        ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView_2, "translationX", traslation);
        animator2.setDuration(500);
        animator2.start();
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView_3, "translationX", traslation * 2);
        animator3.setDuration(500);
        animator3.start();
        ObjectAnimator animator4 = ObjectAnimator.ofFloat(imageView_4, "translationX", traslation * 3);
        animator4.setDuration(500);
        animator4.start();
        ObjectAnimator animator5 = ObjectAnimator.ofFloat(imageView_5, "translationX", traslation * 4);
        animator5.setDuration(500);
        animator5.start();

    }

    public void startAnimation2() {

        ObjectAnimator animator = ObjectAnimator.ofFloat(imageView_back, "alpha", 1f, 0f, 1f);
        animator.setDuration(2000);
        animator.start();
    }

    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            runOnUiThread(new Runnable() {
                public void run() {
                    startAnimation2();
                }
            });
        }
    };

}
